package handlers

import (
	"github.com/gin-gonic/gin"
	"news-hub/models"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	image := c.PostForm("image")
	if title == "" || content == "" || image == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.News{
		Title:   title,
		Content: content,
		Image:   image,
	}
	err := models.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "发布失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "发布成功",
	})
	return

}

func List(c *gin.Context) {
	var news []models.News
	models.DB.Find(&news).Debug()
	c.JSON(200, gin.H{
		"code": 500,
		"list": news,
		"msg":  "新闻列表成功",
	})
	return
}
